package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.ActFeatureEntity;
import com.dataart.training.crimequalification.entity.CriminalActEntity;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ObjectiveSideDAOImpl implements ObjectiveSideDAO {

    //----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    //----------------------------------------------------------------

    /**
     * @return
     */
    @Override
    public List<CriminalActEntity> getAllCriminalActs() {
        return sessionFactory.getCurrentSession()
                .createQuery("from CriminalActEntity order by codeName").list();
    }

    /**
     * @return
     */
    @Override
    public List<ActFeatureEntity> getAllActFeatures() {
        return sessionFactory.getCurrentSession()
                .createQuery("from ActFeatureEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public CriminalActEntity getCriminalActByCodeName(String codename) {
        return (CriminalActEntity) sessionFactory.getCurrentSession().
                createCriteria(CriminalActEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public ActFeatureEntity getAllActFeaturesByCodename(String codename) {
        return (ActFeatureEntity) sessionFactory.getCurrentSession().
                createCriteria(ActFeatureEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

}
